package com.china08.yxyapi.api.manage.stu.mystudy.huixue.comm;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.questionbank.YxyQuesbankSubject;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.huixue.comm.YxyQuesbankSubjectRepository;
import com.china08.yxyapi.repository.db.huixue.comm.YxyQuesbankVersionRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SubjectSrhByStageController extends YxyApiBaseController {

    @Autowired
    private YxyQuesbankSubjectRepository yqsr;

    @Autowired
    private YxyQuesbankVersionRepository yqvr;
    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @ApiOperation(value = "根据学段获得学科")
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/huixue/comm/srhsubbystage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(@RequestBody ReqModel reqModel) {

        List<RespModel> list = new ArrayList<SubjectSrhByStageController.RespModel>();
        List<String> listSubjectId = yqvr.findSubjectIdByStageIdGroupBySubjectId(reqModel.phase);
        if (listSubjectId.size() > 0) {
            List<YxyQuesbankSubject> subjects = yqsr.findByIdInOrderByOrderNum(listSubjectId);
            for (YxyQuesbankSubject subject : subjects) {
                RespModel model = new RespModel();
                model.id = subject.getId();
                model.name = subject.getName();
                List<YxyHuixuePlan> planlist = yxyHuixuePlanRepository
                        .findByStudentIdAndStatusAndSubject(reqModel.studentId, 1, subject.getId());
                if (planlist != null && planlist.size() > 0) {// 该学生存在该学科下正在进行中的学习计划
                    model.isExistsOrnot = 1;
                } else {
                    model.isExistsOrnot = 0;
                }
                list.add(model);
            }
        }
        return list;
    }

    protected static class RespModel {
        public String id;// 学科id
        public String name;// 学科名
        public Integer isExistsOrnot;// 该学生在当前学科下是否存在正在进行中的学习计划(0不存在 1存在)
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String phase;// 学段id
        public String studentId;// 学生id

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "请指定学段");
        }

    }

}
